package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.NewContentBean;
import com.qianfeng.qlm.handforireader.util.BitmapHelp;
import com.qianfeng.qlm.handforireader.util.Constant;

import java.lang.ref.PhantomReference;
import java.util.List;

/**
 * Created by LinXiangLin on 2015/3/25.
 */
public class NewContentRecycleAdapter extends RecyclerView.Adapter<NewContentRecycleAdapter.ViewHolder>{

    private Context context;
    private List<NewContentBean> data;

    public NewContentRecycleAdapter(Context context, List<NewContentBean> data) {
        this.context = context;
        this.data = data;
        BitmapHelp.initUtils(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.new_item_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (data != null && data.size() > 0){
            NewContentBean contentBean = data.get(position);
            String pagerPic = contentBean.getPager_pic();
            if (pagerPic != null){

                if (!pagerPic.startsWith("http://") && !pagerPic.startsWith("https://")){
                    pagerPic = Constant.URL_SUFFIX + pagerPic;
                }

                BitmapHelp.getUtils().display(holder.imgContent,pagerPic,
                        new BitmapLoadCallBack<ImageView>() {
                            @Override
                            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                                imageView.setImageBitmap(bitmap);
                            }

                            @Override
                            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                                imageView.setImageDrawable(drawable);
                            }
                        }
                        );
            }
        }
    }

    public void setData(List<NewContentBean> data){
        if (data != null){
            this.data.clear();
            this.data.addAll(data);
            notifyItemRangeInserted(0,data.size());
        }
    }

    @Override
    public int getItemCount() {
        int ret = 0;
        if (data != null){
            ret = data.size();
        }

        return ret;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView imgContent;
        public ViewHolder(View itemView) {
            super(itemView);
            imgContent = (ImageView) itemView.findViewById(R.id.item_content_image);
        }
    }
}
